package org.user.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.user.dao.UserDao;
import org.user.entity.User;

@WebServlet("/addServlet")
 

public class AddUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
     
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //设置编码
                request.setCharacterEncoding("utf-8");
                response.setCharacterEncoding("utf-8");
                //获取从页面中提交过来的数据
                int id = Integer.parseInt(request.getParameter("id")) ;//数据类型转换
                String userName = request.getParameter("name");
                String userPass = request.getParameter("pass");    
                
                User user = new User();
                
    			try {
					BeanUtils.populate(user, request.getParameterMap());
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
 
                
                //调用dao方法
                UserDao dao = new UserDao();
                int i=dao.addUser(user);
                if(i==1){
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                }
    }

}